package com.lzh.base.handle;

import com.lzh.commonutils.CommonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理类
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public CommonResult error(Exception e){
        e.printStackTrace();
        return CommonResult.error();
    }

    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public CommonResult error(ArithmeticException e){
        e.printStackTrace();
        return CommonResult.error().message("执行了自定义异常");
    }

    /**
     * 自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(MyException.class)
    @ResponseBody
    public CommonResult error(MyException e) {
        e.printStackTrace();
        return CommonResult.error().message(e.getMessage()).code(e.getCode());
    }
}
